<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/hudie.png" alt="停车管理系统" />
        <span>停车管理系统</span>
      </div>
      <div class="user">
        <img :src="userImgUrl" />
        <label>当前用户：{{ currentUser }}</label>
      </div>
    </el-header>

    <!-- 页面主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px' ">
          <div class="toggle-button" @click="toggleCollapse()">|||</div>
        <!-- 侧边栏菜单区域 -->
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409EFF"
          :unique-opened="true"
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in meunList"
            :key="item.id"
          >
            <!-- 一级菜单的模板区域 -->
            <template slot="title">
              <!-- 图片 -->
              <i :class="iconObj[item.id]"></i>
              <!-- 文本 -->
              <span>{{ item.name }}</span>
            </template>

            <!-- 二级菜单 -->
            <el-menu-item
              :index="resource.url"
              v-for="resource in item.resourceList"
              :key="resource.id"
              @click="saveNavState(resource.url)"
            >
              <template slot="title">
                <!-- 图片 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{ resource.name }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧 -->
      <el-main>
          <!-- 路由占位符 -->
          <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      currentUser: '',
      userImgUrl: '',
      //   左侧菜单数据
      meunList: [],
      iconObj: {
        9: 'el-icon-setting',
        10: 'el-icon-s-shop',
        11: 'el-icon-s-custom',
        12: 'el-icon-truck',
        13: 'el-icon-timer',
        14: 'el-icon-s-tools',
      },
      // 是否折叠
      isCollapse: false,
      activePath:''
    }
  },
  mounted() {
   this.getCurrentUserInfo()
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logOut() {
      this.$cookie.delete('token')
      this.$router.push('/login')
    },
    getCurrentUserInfo() {
      // 获取当前登录用户的信息
      this.$http.get('admin/info').then((res) => {
        this.currentUser = res.data.username
        this.userImgUrl = res.data.icon
      })
    },
    getMenuList() {
      // 获取所有菜单
      this.$http.get('/resourceCategory/resourceAndCategory').then((res) => {
        this.meunList = res.data
      })
    },
    toggleCollapse() {
        // 点击按钮，切换菜单的折叠与展开
        this.isCollapse = this.isCollapse ? false : true
    },
    saveNavState(activePath) {
        // 保存导航栏激活状态
        this.activePath = activePath
        window.sessionStorage.setItem('activePath', activePath)
    }
  },
}
</script>




<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center; // 居中
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}

.el-header img {
  height: 65px;
  width: 65px;
  margin-right: 15px;
}
.el-header .user {
  padding-right: 35px;
  font-size: 15px;
  color: #eaedf1;
  font-weight: 100;
}

.el-header .user img {
  width: 45px;
  height: 45px;
  border-radius: 50%;
}
.el-aside {
  background-color: #333744;
  .el-menu {
    border: none;
  }
}
.el-main {
  background-color: #eaedf1;
}
.iconfont {
  margin-right: 10px;
}
.toggle-button {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
